package com.omronservice.algorithm;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: MeanAlgorithm
 * @Author: lfr
 * @CreateDate: 2019/09/02$ 11:19$
 * @UpdateUser: lfr
 * @UpdateDate: 2019/09/02$ 11:19$
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public class StdDevAlgorithm {
    //总值
    public static double countSum(double[] p1) {
        double sum = 0;
        for (int i = 0; i < p1.length; i++)
            sum += p1[i] ;

        return sum;
    }
    //平均值
    public static double countMean(double[] p1) {
        double sum =countSum(p1);
        return sum/p1.length;
    }

    //标准差σ=sqrt(s^2)
    public static double StandardDiviation(double[] x) {

        return Math.sqrt(Variance(x));
    }

    //方差s^2=[(x1-x)^2 +...(xn-x)^2]/n 或者s^2=[(x1-x)^2 +...(xn-x)^2]/(n-1)
    public static double Variance(double[] x) {
        int m = x.length;
        double dAve = countMean(x);//求平均值
        double dVar = 0;
        for (int i = 0; i < m; i++) {//求方差
            dVar += (x[i] - dAve) * (x[i] - dAve);
        }
        return dVar / m;
    }
    //偏移系数
    public static Map varianceK(double[] x) {
        int m = x.length;
        double dAve = countMean(x);//求平均值
        Map reK=new HashMap();
        for (int i = 0; i < m; i++) {
            double mu=(x[i]-dAve)>0?(x[i]-dAve):-(x[i]-dAve);
            double k=mu/(m/2);
            reK.put(x[i],k);
        }
        return reK;
    }
}
